package com.example.cd5160866.urlapp.modelv1;

import android.content.Context;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cd5160866 on 16/5/10.
 */
public abstract class ModelRecyclerAdapter<T> extends RecyclerView.Adapter<ModelRecyclerAdapter.ModelViewHolder> {
    protected Context mContext;
    protected int mLayoutId;
    protected List<T> mDatas = new ArrayList<>();


    public ModelRecyclerAdapter(Context context, int layoutId) {
        mContext = context;
        mLayoutId = layoutId;
    }

    @Override
    public ModelViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        ModelViewHolder viewHolder = ModelViewHolder.get(mContext, parent, mLayoutId);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ModelViewHolder holder, int position) {
        holder.updatePosition(position);
        update(holder, mDatas.get(position), position);
    }

    public abstract void update(ModelViewHolder holder, T t, int position);

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public void add(int positon, T data) {
        mDatas.add(positon, data);
        notifyItemInserted(positon);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
             notifyDataSetChanged();
            }
        },300);

    }

    public void addAll(List<T> datas) {
        mDatas.addAll(datas);
        notifyDataSetChanged();
    }

    public void clear() {
        mDatas.clear();
        notifyDataSetChanged();
    }

    public void remove(int position) {
        mDatas.remove(position);
        notifyItemRemoved(position);
    }

    public static class ModelViewHolder extends RecyclerView.ViewHolder {
        private SparseArray<View> mViews;
        private View mConvertView;
        private int mPosition;

        public ModelViewHolder(View itemView) {
            super(itemView);
            mConvertView = itemView;
            mViews = new SparseArray<View>();
        }


        public static ModelViewHolder get(Context context, ViewGroup parent, int layoutId) {

            View itemView = LayoutInflater.from(context).inflate(layoutId, parent,
                    false);
            ModelViewHolder holder = new ModelViewHolder(itemView);
            return holder;
        }

        /**
         * 通过viewId获取控件
         */
        public <T extends View> T getViewbyId(int viewId) {
            View view = mViews.get(viewId);
            if (view == null) {
                view = mConvertView.findViewById(viewId);
                mViews.put(viewId, view);
            }
            return (T) view;
        }

        public void updatePosition(int position) {
            this.mPosition = position;
        }

        public int getCurrentPosition() {
            return mPosition;
        }

        public ModelViewHolder setOnItemClickListener(View.OnClickListener listener) {
            mConvertView.setOnClickListener(listener);
            return this;
        }

        public ModelViewHolder setOnItemLongClickListener(View.OnLongClickListener listener) {
            mConvertView.setOnLongClickListener(listener);
            return this;
        }

    }

}
